package config

import (
	"fmt"
	"github.com/spf13/viper"
	"os"
)

var GConfig Config

type Config struct {
	Server     ServerConfig     `yaml:"server"`
	Datasource DatasourceConfig `yaml:"datasource"`
	Jwt        JwtConfig        `yaml:"jwt"`
}

type ServerConfig struct {
	Port int    `yaml:"port"`
	Host string `yaml:"host"`
}

type DatasourceConfig struct {
	Port     int    `yaml:"port"`
	Host     string `yaml:"host"`
	Database string `yaml:"database"`
	Username string `yaml:"username"`
	Password string `yaml:"password"`
	Charset  string `yaml:"charset"`
	Loc      string `yaml:"loc"`
}

type JwtConfig struct {
	ExpTime int64  `yaml:"expTime"`
	Secret  string `yaml:"secret"`
	Issuer  string `yaml:"issuer"`
}

func init() {
	workDir, _ := os.Getwd()
	viper.SetConfigName("config")
	viper.SetConfigType("yaml")
	viper.AddConfigPath(workDir)

	if err := viper.ReadInConfig(); err != nil {
		fmt.Printf("读取配置文件时出错, %s", err)
	}

	err := viper.Unmarshal(&GConfig)
	if err != nil {
		fmt.Printf("结构化时出错， %s", err)
	}
}
